//
//  CircleTransition.m
//  AnimationDemo
//
//  Created by 马滕亚 on 2020/6/14.
//  Copyright © 2020 M.Y.A. All rights reserved.
//

#import "CircleTransition.h"
#import "OneViewController.h"

@interface CircleTransition()<CAAnimationDelegate>

@property (nonatomic, strong) id<UIViewControllerContextTransitioning> context;

@end

@implementation CircleTransition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 0.8;
}

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
    /**
     动画拆分
     1、 2个圆
     2、添加动画
     */
    
    // 1 持有上下文
    self.context = transitionContext;
    // 2 获取view的容器
    UIView *containView = transitionContext.containerView;
    // 3 获取toVC的view 添加到容器里面
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [containView addSubview:toVC.view];
    // 4 添加动画
    
    OneViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [containView addSubview:fromVC.view];
    UIButton *button = fromVC.oneBtn;
    // 画一个小圆(大小圆的中心是一样的)
    UIBezierPath *smallPath = [UIBezierPath bezierPathWithOvalInRect:button.frame];
    
    // 大圆 中心点
    CGPoint centerP;
    centerP = button.center;
    // 大圆 半径
    CGFloat cornerRadius;
    // 通过象限计算半径
    if (button.frame.origin.x > CGRectGetWidth(toVC.view.frame)/2) {
        if (button.frame.origin.y < CGRectGetHeight(toVC.view.frame)/2) {
            // 一象限
            CGFloat y = CGRectGetHeight(toVC.view.bounds) - button.frame.origin.y;
            cornerRadius = sqrtf(button.center.x * button.center.x+y*y);
        } else {
            // 四象限
            CGFloat y = button.center.y;
            cornerRadius = sqrtf(button.center.x * button.center.x+y*y);
        }
    } else {
        if (button.frame.origin.y < CGRectGetHeight(toVC.view.frame)/2) {
            // 二象限
            CGFloat x = CGRectGetWidth(toVC.view.bounds) - button.frame.origin.x;
            CGFloat y = CGRectGetHeight(toVC.view.bounds) - button.frame.origin.y;
            cornerRadius = sqrtf(x*x+y*y);
        } else {
            // 三象限
            cornerRadius = sqrtf(button.center.x * button.center.x+button.center.y*button.center.y);
        }
    }
    
    // 画大圆
    UIBezierPath *bigPath = [UIBezierPath bezierPathWithArcCenter:centerP radius:cornerRadius startAngle:0 endAngle:M_PI*2 clockwise:true];
    
    // 将path添加到layer上面
    CAShapeLayer *shapLayer = [CAShapeLayer layer];
    shapLayer.path = bigPath.CGPath;
    // 蒙版
    toVC.view.layer.mask = shapLayer;
    // 设置动画
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"path";
    animation.fromValue = (id)smallPath.CGPath;
//    animation.toValue = bigPath;
    animation.delegate = self;
    
    [shapLayer addAnimation:animation forKey:nil];
    
}


#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [self.context completeTransition:true];
    UIViewController *toVC = [self.context viewControllerForKey:UITransitionContextToViewControllerKey];
    toVC.view.layer.mask = nil;
}


















@end
